﻿$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        datatype: "json",
        url: "../../Ajax/PlsettingAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblId' id='lbl" + k + "'>";
                    tbody += v.ID;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblName' id='lbl" + k + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblOvertopValue' id='lbl" + k + "'>";
                    tbody += v.OvertopValue;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblUnderValue' id='lbl" + k + "'>";
                    tbody += v.UnderValue;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblHandCount' id='lbl" + k + "'>";
                    tbody += v.HandCount;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblInitValue' id='lbl" + k + "'>";
                    tbody += v.InitValue;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNormValue' id='lbl" + k + "'>";
                    tbody += v.NormValue;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblTuneValue' id='lbl" + k + "'>";
                    tbody += v.TuneValue;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $("#btnAdd").click(function () {
        Edit(null);
    })

})
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortNum = $(lbl).parent().parent().find("lable[name=lblName]");
    var sortId = $(lbl).parent().parent().find("lable[name=lblId]").text();
    var content = "";
    if (lbl != null) {
        content = "<table><tr><td>亏赢设置名称：<input type='text' id='txtName' value='" + $(lbl).parent().parent().find("lable[name=lblName]").first().text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>高概率值：<input type='text' id='txtOvertopValue' value='" + $(lbl).parent().parent().find("lable[name=lblOvertopValue]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>低概率值：<input type='text' id='txtUnderValue' value='" + $(lbl).parent().parent().find("lable[name=lblUnderValue]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手数：<input type='text' id='txtHandCount' value='" + $(lbl).parent().parent().find("lable[name=lblHandCount]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>初始值：<input type='text' id='txtInitValue' value='" + $(lbl).parent().parent().find("lable[name=lblInitValue]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>标准值：<input type='text' id='txtNormValue' value='" + $(lbl).parent().parent().find("lable[name=lblNormValue]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>每充值（元）盈亏值调整：<input type='text' id='txtTuneValue' value='" + $(lbl).parent().parent().find("lable[name=lblTuneValue]").text() + "' style='width:200px;float:right' /></td></tr></table>"
    }
    else {
        content = "<table><tr><td>亏赢设置名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>高概率值：<input type='text' id='txtOvertopValue' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>低概率值：<input type='text' id='txtUnderValue' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>手数：<input type='text' id='txtHandCount' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>初始值：<input type='text' id='txtInitValue' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>标准值：<input type='text' id='txtNormValue' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>每充值（元）盈亏值调整：<input type='text' id='txtTuneValue' style='width:200px;float:right' /></td></tr></table>"
    }

    $.dialog({
        title: "更新亏盈设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            var objectOvertopValue = $("#txtOvertopValue").val();
                            var objectUnderValue = $("#txtUnderValue").val();
                            var objectHandCount = $("#txtHandCount").val();
                            var objectInitValue = $("#txtInitValue").val();
                            var objectNormValue = $("#txtNormValue").val();
                            var objectTuneValue = $("#txtTuneValue").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入模板名称！");
                                return false;
                            }
                            AddMould(objectName, lbl, $(txtName), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}

function AddMould(objectName, contr, sortNum, sortId) {
    var objectId = sortId;
    var numVal = $(sortNum).val();
    var objectName = $("#txtName").val();
    var objectOvertopValue = $(sortNum).parent().parent().parent().find("#txtOvertopValue").val();
    var objectUnderValue = $(sortNum).parent().parent().parent().find("#txtUnderValue").val();
    var objectHandCount = $(sortNum).parent().parent().parent().find("#txtHandCount").val();
    var objectInitValue = $(sortNum).parent().parent().parent().find("#txtInitValue").val();
    var objectNormValue = $(sortNum).parent().parent().parent().find("#txtNormValue").val();
    var objectTuneValue = $(sortNum).parent().parent().parent().find("#txtTuneValue").val();
    numVal = numVal == "" ? 0 : numVal;
    var data = contr == null ? { action: "Add", name: objectName, OvertopValue: objectOvertopValue, UnderValue: objectUnderValue, HandCount: objectHandCount, InitValue: objectInitValue, NormValue: objectNormValue, TuneValue: objectTuneValue} :
              { action: "Update", id: objectId, name: objectName, OvertopValue: objectOvertopValue, UnderValue: objectUnderValue, HandCount: objectHandCount, InitValue: objectInitValue, NormValue: objectNormValue, TuneValue: objectTuneValue };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/PlsettingAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {

            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr>";
                tbody += "<td><lable name='lblId'>";
                tbody += data;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblName'>";
                tbody += objectName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblOvertopValue'>";
                tbody += objectOvertopValue;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblUnderValue'>";
                tbody += objectUnderValue;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblHandCount'>";
                tbody += objectHandCount;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblInitValue'>";
                tbody += objectInitValue;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblNormValue'>";
                tbody += objectNormValue;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblTuneValue'>";
                tbody += objectTuneValue;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'>"
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblName]").first().text(objectName);
                $(contr).parent().parent().find("lable[name=lblOvertopValue]").first().text(objectOvertopValue);
                $(contr).parent().parent().find("lable[name=lblUnderValue]").first().text(objectUnderValue);
                $(contr).parent().parent().find("lable[name=lblHandCount]").first().text(objectHandCount);
                $(contr).parent().parent().find("lable[name=lblInitValue]").first().text(objectInitValue);
                $(contr).parent().parent().find("lable[name=lblNormValue]").first().text(objectNormValue);
                $(contr).parent().parent().find("lable[name=lblTuneValue]").first().text(objectTuneValue);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
